package de.reminder.client.widgets;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;

public class TimePicker2 extends Composite {
	
	private HorizontalPanel main;
	private ListBox listbox;
	private Label label;
	private boolean readOnly = false;
	
	public TimePicker2(String[] values){
		main = new HorizontalPanel();
		main.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM);
		label = new Label(values[0]);
		label.getElement().setClassName("Handwritten");
		label.getElement().getStyle().setPaddingTop(3, Unit.PX);
		listbox = new ListBox();
		
		for(String s : values){
			listbox.addItem(s);
		}
		
		main.add(label);
		main.add(listbox);
		listbox.setVisible(false);
		
		label.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				if(readOnly)
					return;
				label.setVisible(false);
				listbox.setVisible(true);
				listbox.setFocus(true);
			}
		});
		
		listbox.addChangeHandler(new ChangeHandler() {
			
			@Override
			public void onChange(ChangeEvent event) {
				label.setVisible(true);
				listbox.setVisible(false);
				label.setText(listbox.getItemText(listbox.getSelectedIndex()));
			}
		});
		
		listbox.addBlurHandler(new BlurHandler() {
			
			@Override
			public void onBlur(BlurEvent event) {
				label.setVisible(true);
				listbox.setVisible(false);
				label.setText(listbox.getItemText(listbox.getSelectedIndex()));
			}
		});
		
		
		
		initWidget(main);
	}
	
	public int getValue(){
		return Integer.parseInt(label.getText());
	}

	public void setReadOnly() {
		readOnly = true;
	}

}
